﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using NOCSort;
using LaserMark;

using System.Reflection;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {

        [DllImport("EncodeDecode.dll", EntryPoint = "test_exception", CallingConvention = CallingConvention.Cdecl)]
        public static extern void test_exception();
        public Bitmap draw_image;
        public Form1()
        {
            InitializeComponent();

            draw_image = new Bitmap(pictureBox.Size.Width, pictureBox.Size.Height);
        }

        private List<BadmarkStructure> generatorBadmarkList()
        {
            List<BadmarkStructure> bds = new List<BadmarkStructure>();

            Random rd = new Random();
            for (int i = 0; i < 9; i++)
            {
                for(int j = 0; j < 10; j++)
                {
                    int y = rd.Next() % 200;
                    int x = rd.Next() % 300;
                    bds.Add(new BadmarkStructure(-x, -y, 5, 10));
                }
            }
            

            return bds;
        }
        private void clearup(ref Graphics g)
        {
            g.Clear(Color.LightGray);
        }
        private void descartesToScreen(int screenw, int screenh, int x, int y, out int nx, out int ny)
        {
            nx = x;
            ny = screenh - y;
        }

        private void descartesToScreen(int screenw, int screenh, double x, double y, out int nx, out int ny)
        {
            x += 300;
            y += 200;

            nx = (int)(x + 0.5);
            ny = screenh - (int)(y + 0.5);

            nx += 40;
            ny -= 40;
        }
        private void test_sort()
        {
            List<BadmarkStructure> bdmarklist = generatorBadmarkList();
            CameraArea[] areas = CameraAreaOptimization.CreateCameraAreasForBadMarkByCameraViewSize(bdmarklist, 60, 60);

            Graphics g = Graphics.FromImage(draw_image);
            clearup(ref g);
            int sw = pictureBox.Size.Width;
            int sh = pictureBox.Size.Height;
            int areas_count = areas.Length;
            Pen drawPen = new Pen(Color.Blue);
            Pen drawPenGreen = new Pen(Color.Green);
            for (int i = 0; i < areas_count; i++)
            {
                int x0, y0, x1, y1, x2, y2, x3, y3;
                descartesToScreen(sw, sh, areas[i].cameraAreaRect.lt_x, areas[i].cameraAreaRect.lt_y, out x0, out y0);
                descartesToScreen(sw, sh, areas[i].cameraAreaRect.rb_x, areas[i].cameraAreaRect.lt_y, out x1, out y1);
                descartesToScreen(sw, sh, areas[i].cameraAreaRect.rb_x, areas[i].cameraAreaRect.rb_y, out x2, out y2);
                descartesToScreen(sw, sh, areas[i].cameraAreaRect.lt_x, areas[i].cameraAreaRect.rb_y, out x3, out y3);

                
                g.DrawLine(drawPen, new Point(x0, y0), new Point(x1, y1));
                g.DrawLine(drawPen, new Point(x1, y1), new Point(x2, y2));
                g.DrawLine(drawPen, new Point(x2, y2), new Point(x3, y3));
                g.DrawLine(drawPen, new Point(x3, y3), new Point(x0, y0));

                Font drawFont = new Font("Arial", 15);//显示的字符串使用的字体
                SolidBrush drawBrush = new SolidBrush(Color.Red);//写字符串用的刷子
                g.DrawString(i.ToString(), drawFont, drawBrush, new PointF((float)(areas[i].cameraAreaRect.c_x +300+ 40), (float)(sh - 200 - areas[i].cameraAreaRect.c_y - 40)));

                int elemCount = areas[i].elementIndexList.Count;
                for (int j = 0; j < elemCount; j++)
                {
                    RectangleStruct rs = areas[i].elementBoundRects[j];
                    descartesToScreen(sw, sh, rs.lt_x, rs.lt_y, out x0, out y0);
                    descartesToScreen(sw, sh, rs.rb_x, rs.lt_y, out x1, out y1);
                    descartesToScreen(sw, sh, rs.rb_x, rs.rb_y, out x2, out y2);
                    descartesToScreen(sw, sh, rs.lt_x, rs.rb_y, out x3, out y3);
                    g.DrawLine(drawPenGreen, new Point(x0, y0), new Point(x1, y1));
                    g.DrawLine(drawPenGreen, new Point(x1, y1), new Point(x2, y2));
                    g.DrawLine(drawPenGreen, new Point(x2, y2), new Point(x3, y3));
                    g.DrawLine(drawPenGreen, new Point(x3, y3), new Point(x0, y0));
                }
            }



            g.Dispose();
            this.pictureBox.Image = draw_image;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            test_sort();
        }
    }
}
